package pers.spj.image.analysis.ui

import pers.spj.image.analysis.iface.OnCancelListener
import pers.spj.image.analysis.iface.OnConfirmListener
import java.awt.Dimension
import java.awt.LayoutManager
import java.awt.event.ActionEvent
import javax.swing.AbstractAction
import javax.swing.Action
import javax.swing.BorderFactory
import javax.swing.Box
import javax.swing.BoxLayout
import javax.swing.JButton
import javax.swing.JPanel

/**
 * @author spj
 * @date 2018/10/18.
 */

class EntryFooter : JPanel {
    private var mOnConfirmListener: OnConfirmListener<String>? = null
    private var mOnCancelListener: OnCancelListener? = null

    constructor(layout: LayoutManager) : super(layout) {}
    constructor(leftButtonName: String, rightButtonName: String) {
        val mCancel = getConfirmOrCancelButton(leftButtonName, object : AbstractAction() {
            override fun actionPerformed(e: ActionEvent) {
                mOnCancelListener!!.onCancel()
            }
        })
        val mConfirm = getConfirmOrCancelButton(rightButtonName, object : AbstractAction() {
            override fun actionPerformed(e: ActionEvent) {
                mOnConfirmListener!!.onConfirm("");
            }
        })
        layout = BoxLayout(this, BoxLayout.X_AXIS)
        border = BorderFactory.createEmptyBorder(10, 10, 10, 10)
        add(mCancel)
        add(Box.createRigidArea(Dimension(30, 0)))
        add(mConfirm)
    }

    fun setOnConfirmListener(listener: OnConfirmListener<String>) {
        mOnConfirmListener = listener;
    }

    fun setOnCancelListener(listener: OnCancelListener) {
        mOnCancelListener = listener;
    }

    private fun getConfirmOrCancelButton(buttonName: String, action: Action): JButton {
        val mConfirm = JButton()
        mConfirm.action = action
        mConfirm.preferredSize = Dimension(120, 32)
        mConfirm.text = buttonName
        mConfirm.isVisible = true
        return mConfirm
    }
}
